package com.liveinstars.common.sort;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 简单的列表排序模板
 * Created by guoyanjiang on 2019-01-27.
 */
public class SortPagingBean<T extends BaseCompareBean> {

    private List<T> list;

    private int totalSize;

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    void initData(List<T> list){
        this.list = list;
        this.totalSize = list.size();
    }

    List<T> sortAndPageing(int page, int size, String orderField, String orderType) {
        List<T> result = list.stream()
                .sorted((o1, o2) -> o1.tryBestToFindCompareValue(orderField).compareTo(o2.tryBestToFindCompareValue(orderField)))
                .skip((page - 1) * size)
                .collect(Collectors.toList());
        if (orderType.equals("desc")) {
            Collections.reverse(result);
        }
        return result;
    }


}
